package ldh.im.fxgui.rpc.service;

import ldh.im.dto.enums.DataSubType;
import ldh.im.dto.enums.DataType;

import java.util.List;

public enum DataTypeEnum {
    group(1,  DataSubTypeEnum.groups("group"), GroupChangeDataService.class),
    session(2,  DataSubTypeEnum.groups("session"), SessionChangeDataService.class),
    groupUser(3,  DataSubTypeEnum.groups("groupUser"), GroupUserChangeDataService.class),
    ;

    private short value;
    private List<DataSubTypeEnum> dataSubTypes;
    private Class<?> handleService;

    private DataTypeEnum(Integer type, List<DataSubTypeEnum> dataSubTypes, Class handleService) {
        this.dataSubTypes = dataSubTypes;
        this.value = type.shortValue();
        this.handleService = handleService;
    }

    public Class<?> getHandleService() {
        return handleService;
    }

    public static DataTypeEnum getInstance(String name) {
        for(DataTypeEnum changeDataType : DataTypeEnum.values()) {
            if (changeDataType.name().equals(name)) {
                return changeDataType;
            }
        }
        throw new RuntimeException("ChangeDataTypeEnum不包含此处理类型: " + name);
    }

    public static DataTypeEnum wrap(DataType dataType) {
        DataTypeEnum dataTypeEnum = getInstance(dataType.name());
        return dataTypeEnum;
    }

    public DataSubTypeEnum wrap(DataSubType dataSubType) {
        for (DataSubTypeEnum subTypeEnum : this.dataSubTypes) {
            if (subTypeEnum.getSubType().equals(dataSubType.name())) {
                return subTypeEnum;
            }
        }
        throw new RuntimeException("DataTypeEnum没有这个枚举值:" + dataSubType.name());
    }
}
